package com.guet;

import javax.swing.*;
import java.awt.event.*;
import java.util.Map;
import java.util.Scanner;

//管理员端
public class rootJFrame extends JFrame {
    static String startName="";
    static String endName="";
    private fileUse f=new fileUse();
    private int logicX=0,logicY=0,realX,realY;
    public Map<Integer,vexsinfo> vexs=new fileUse().getAllVex();
    public void showStartAndEnd(){
        vexsinfo v=f.getVexByName(startName);
        vexsinfo v1=f.getVexByName(endName);
        MyJFrame.drawPanel.showPoints.clear();
        if(!startName.equals(""))MyJFrame.drawPanel.showPoints.put(v.id,v);
        if(!endName.equals(""))MyJFrame.drawPanel.showPoints.put(v1.id,v1);
        MyJFrame.drawPanel.repaint();
    }
    public rootJFrame(int x){
        setLayout(null);
        setSize(600,700);
        setLocation(x,0);
        JComboBox startCom=new JComboBox();
        JComboBox endCom=new JComboBox();
        JButton startBtn=new JButton("查找");
        JButton endBtn=new JButton("查找");
        JLabel startLabel=new JLabel("起点:");
        JLabel endLabel=new JLabel("终点:");
        JLabel transportWay=new JLabel("交通方式:");
        ButtonGroup buttonGroup=new ButtonGroup();
        JRadioButton walk=new JRadioButton("步行");
        JRadioButton driving=new JRadioButton("开车");
        JLabel periodLabel=new JLabel("时间段");
        JComboBox periodCom=new JComboBox();
        JButton setDistaceBtn=new JButton("设置两点间的距离(加边)");
        JButton showBtn=new JButton("显示所有点和边");
        JButton addVexBtn=new JButton("新增结点(双击地图选点)");
        startCom.setBounds(85,130,350,25);
        endCom.setBounds(85,200,350,25);
        startBtn.setBounds(465,130,60,25);
        endBtn.setBounds(465,200,60,25);
        startLabel.setBounds(25,130,50,25);
        endLabel.setBounds(25,200,60,25);
        transportWay.setBounds(25,250,60,25);
        walk.setBounds(105,250,55,25);
        driving.setBounds(235,250,55,25);
        periodLabel.setBounds(25,300,55,25);
        periodCom.setBounds(85,300,350,25);
        setDistaceBtn.setBounds(55,410,400,50);
        showBtn.setBounds(55,480,400,50);
        addVexBtn.setBounds(55,550,400,50);

        startCom.addItem("选择起始位置");
        endCom.addItem("选择终点位置");
        for(vexsinfo v:f.getAllVex().values()){
            startCom.addItem(v.name);
            endCom.addItem(v.name);
        }
        String times[]={"当前时间","00:00 ~ 11:15","11:15 ~ 12:30","12:30 ~ 14:00","14:30 ~ 17:35","17:35 ~ 18:35","18:35 ~ 20:00","20:00 ~ 21:00","21:00 ~ 24:00"};
        for(String t:times){
            periodCom.addItem(t);
        }
        startBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                searchNameJFrame searchnamejframe=new searchNameJFrame(getWidth(),0);
                searchnamejframe.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosed(WindowEvent e) {
                        super.windowClosed(e);
                        showStartAndEnd();
                    }
                });
            }
        });
        endBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                searchNameJFrame searchnamejframe=new searchNameJFrame(getWidth(),1);
                searchnamejframe.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosed(WindowEvent e) {
                        super.windowClosed(e);
                        //查找窗口关闭后
                        showStartAndEnd();
                    }
                });
            }
        });
        startCom.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //起点选择发生变化后
                startName=startCom.getSelectedItem().toString();
                showStartAndEnd();
            }
        });
        endCom.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //终点选择发生变化后
                endName=endCom.getSelectedItem().toString();
                showStartAndEnd();
            }
        });
        setDistaceBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                f.setTwoPointDistance(startCom.getSelectedItem().toString(),endCom.getSelectedItem().toString());
            }
        });
        showBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                MyJFrame.drawPanel.shortest.clear();
                MyJFrame.drawPanel.alg.mGraph=new MGraph();
                if(MyJFrame.drawPanel.showAll==1) {
                    MyJFrame.drawPanel.showAll=0;
                    showBtn.setText("取消显示所有结点及边");
                }else{
                    MyJFrame.drawPanel.showAll=1;
                    showBtn.setText("显示所有结点及边");
                }
                MyJFrame.drawPanel.repaint();
            }
        });
        MyJFrame.drawPanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if(e.getClickCount()==1){
                    vexsinfo v=new vexsinfo();
                    v.logicx=e.getX();
                    v.logicy=e.getY();
                    logicX=v.logicx;
                    logicY= v.logicy;
                    v.name="新结点逻辑坐标";
                    MyJFrame.drawPanel.showPoints.put(0,v);
                    MyJFrame.drawPanel.repaint();
                }else{
                    vexsinfo v=new vexsinfo();
                    v.logicx=e.getX();
                    v.logicy=e.getY();
                    realX=v.logicx;
                    realY=v.logicy;
                    v.name="新结点真实坐标";
                    MyJFrame.drawPanel.showPoints.put(1,v);
                    MyJFrame.drawPanel.repaint();
                }
            }
        });
        addVexBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if(logicX==0||logicY==0){
                    JOptionPane.showMessageDialog(null,"先在地图双击选逻辑位置和单击选实际位置");
                    return;
                }
                vexsinfo v=new vexsinfo();
                System.out.println("输入过程中输入单独的'.'可退出新增结点");
                Scanner sc=new Scanner(System.in);

                System.out.print("请输入新结点名称: ");
                v.name=sc.next();
                if(v.name.equals("."))return;
                System.out.print("请输入新结点是否可通车y/n: ");
                String c=sc.next();
                v.drive=c.equals("y")?1:0;
                if(c.equals("."))return;
                System.out.println("输入相邻上下左右结点名字，不存在则-1");
                String [] dir=new String[5];
                System.out.print("上结点：");dir[0]=sc.next();if(dir[0].equals("."))return;
                System.out.print("下结点：");dir[1]=sc.next();if(dir[1].equals("."))return;
                System.out.print("左结点：");dir[2]=sc.next();if(dir[2].equals("."))return;
                System.out.print("右结点：");;dir[3]=sc.next();if(dir[3].equals("."))return;
                for(int i=0;i<4;i++){  //修改新结点的对应结点对应自己
                    if(dir[i].equals("-1")){
                        switch (i){
                            case 0:v.top=-1;break;
                            case 1:v.bottom=-1;break;
                            case 2:v.left=-1;break;
                            case 3:v.right=-1;break;
                        }
                        continue;
                    }
                    for(vexsinfo vex:f.getAllVex().values()){
                        if(vex.name.equals(dir[i])){
                            switch (i){
                                case 0:v.top=vex.id;break;
                                case 1:v.bottom=vex.id;break;
                                case 2:v.left=vex.id;break;
                                case 3:v.right=vex.id;break;
                            }
                        }
                    }
                }
                v.position=v.name.contains("路口")?0:1;
                v.logicx=logicX;
                v.logicy=logicY;
                if(v.position==0){
                    MyJFrame.drawPanel.showPoints.remove(1);
                    MyJFrame.drawPanel.repaint();
                    v.realx=logicX;
                    v.realy=logicY;
                }else{
                    v.realx=realX;
                    v.realy=realY;
                }
                int max=0;
                for(vexsinfo vex:f.getAllVex().values()){
                    if(vex.id>max){
                        max=vex.id;
                    }
                }
                v.id=max+1;

                sc.nextLine();
                System.out.print("请输入繁忙时间点如11:35(不输入默认无): ");
                c=sc.nextLine();
                if(!c.equals("")){
                    //写入存在时间段繁忙的队伍文件中
                    new fileUse().addCrowded(v.name);
                }else v.croded_level=1.0F;

                System.out.print("地点简介(回车->略过): ");
                c=sc.nextLine();
                v.introduction=c;
                if(c.equals("."))return;
                //文件更新邻结点，文件增加结点
                f.updateVexDir(v.id,dir);
                f.addVex(v);
            }
        });
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        add(addVexBtn);
        add(showBtn);
        add(setDistaceBtn);
        add(periodLabel);
        add(periodCom);
        buttonGroup.add(walk);
        buttonGroup.add(driving);
        add(transportWay);
        add(walk);add(driving);
        add(startLabel);
        add(endLabel);
        add(startCom);
        add(endCom);
        add(startBtn);
        add(endBtn);
        setVisible(true);
    }
}
